package main

import (
	"go.uber.org/zap"
	"live-core/core"
	"live-core/logging"
	"live-core/server"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	//流程管理初始化
	core.InitStreamManager()
	//日志刷新,写入磁盘
	defer logging.ZapLogSync()
	//启动http服务
	go server.HTTPAPIServer()
	//开启 rtsp 服务
	go server.RTSPServer()
	go core.StreamManager.ChannelRunAll()

	//停机信号
	signalChanel := make(chan os.Signal, 1)
	done := make(chan bool, 1)
	signal.Notify(signalChanel, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		sig := <-signalChanel
		logging.ZapLog.Info("服务接收信令", zap.Any("sig", sig))
		done <- true
	}()
	logging.ZapLog.Info("服务启动成功!!!")
	//stop()
	<-done
	core.StreamManager.StreamStopAll()
	time.Sleep(2 * time.Second)
	logging.ZapLog.Info("服务停止!!!")
}

//func stop() {
//	timeout := time.After(time.Second * 20)
//	finish := make(chan bool)
//	count := 1
//	go func() {
//		for {
//			select {
//			case <-timeout:
//				finish <- true
//				return
//			default:
//				count++
//			}
//			time.Sleep(time.Second * 1)
//		}
//	}()
//	<-finish
//	panic("试用结束")
//}
